package com.example.skillcinema.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.skillcinema.databinding.SeasonSerialItemBinding
import com.example.skillcinema.db.models.Episode
import com.example.skillcinema.viewholders.SeasonSerialItemViewHolder

class SeasonSerialRecyclerViewAdapter : RecyclerView.Adapter<SeasonSerialItemViewHolder>() {
    var values: List<Episode> = listOf()

    fun setData(list: List<Episode>) {
        values = list
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup, viewType: Int
    ): SeasonSerialItemViewHolder = SeasonSerialItemViewHolder(
        SeasonSerialItemBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
    )

    override fun onBindViewHolder(
        holder: SeasonSerialItemViewHolder, position: Int
    ) {
        val item = values[position]
        with(holder.binding) {
            serialNameTextView.text = buildString {
                append(item.episodeNumber)
                append("  ")
                append(item.nameRu)
            }
            releaseDateTextView.text = item.releaseDate.toString()
        }
    }

    override fun getItemCount(): Int = values.count()
}
